//
//  ImitateViewController.m
//  Drawboard
//
//  Created by A-01 on 2020/6/24.
//  Copyright © 2020 Mac. All rights reserved.
//

#import "ImitateViewController.h"
#import "DrawTouchPointView.h"

@interface ImitateViewController ()

@property (nonatomic, weak)UIButton * saveButton;
@property (nonatomic, weak)UIView * colorView;
@property (nonatomic, strong)NSArray * colorArray;
@property (nonatomic, strong)UIView * backView;
@property (nonatomic, strong)DrawTouchPointView * drawView;
@property (nonatomic, weak)UIButton * eraserButton;
@property (nonatomic, strong)UIButton * penOneButton;
@property (nonatomic, strong)UIButton * penTwoButton;
@property (nonatomic, strong)UIButton * penThreeButton;
@property (nonatomic, strong)UIButton * penFourButton;
@property (nonatomic, strong)UIView * eraserRectView;
@property (nonatomic, strong)UIView * oneRectView;
@property (nonatomic, strong)UIView * twoRectView;
@property (nonatomic, strong)UIView * threeRectView;
@property (nonatomic, strong)UIView * fourRectView;
@property (nonatomic, weak)UIToolbar *toolbar;
@property (nonatomic, weak)UIView * whiteView;
@property (nonatomic, strong)UIImage * image;

@end

@implementation ImitateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
}


- (void)setupUI{
    
    UIButton * saveButton = [UIButton new];
    
    [saveButton setImage:[UIImage imageNamed:@"save"] forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(clickSaveButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.saveButton = saveButton;
    
    [self.view addSubview:self.saveButton];
    
    UIView * colorView = [UIView new];
    
    colorView.backgroundColor = ZRWhiteColor;
    colorView.layer.cornerRadius = 20;
    colorView.layer.borderColor = [ZRBlackColor CGColor];
    colorView.layer.borderWidth = 1.5;
    
    self.colorView = colorView;
    
    [self.view addSubview:self.colorView];
    
    UIView * backView = [UIView new];
   
    backView.backgroundColor = ZRWhiteColor;
    backView.layer.cornerRadius = 20;
    backView.layer.borderColor = [ZRBlackColor CGColor];
    backView.layer.borderWidth = 2;
    backView.userInteractionEnabled = YES;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;

    [self.view addSubview:self.backView];
    
    
    self.drawView = [DrawTouchPointView new];
    
    self.drawView.backgroundColor = ZRWhiteColor;
    [self.drawView setStrokeColor:[UIColor colorWithHexString:self.colorArray[0]]];
    [self.drawView setStrokeWidth:2];
    
    [self.backView addSubview:self.drawView];
    
    [self setupColorButton];
    
    UIButton * eraserButton = [UIButton new];
    [eraserButton setImage:[UIImage imageNamed:@"eraser"] forState:UIControlStateNormal];
    [eraserButton addTarget:self action:@selector(clickToolButton:) forControlEvents:UIControlEventTouchUpInside];

    self.eraserButton = eraserButton;
    
    [self.backView addSubview:self.eraserButton];
    
    self.penOneButton = [self setupButtonWithImg:@"pen_one"];
    [self.backView addSubview:self.penOneButton];
    self.penTwoButton = [self setupButtonWithImg:@"pen_two"];
    [self.backView addSubview:self.penTwoButton];
    self.penThreeButton = [self setupButtonWithImg:@"pen_three"];
    [self.backView addSubview:self.penThreeButton];
    self.penFourButton = [self setupButtonWithImg:@"pen_four"];
    [self.backView addSubview:self.penFourButton];
    
    self.eraserRectView = [self setupRectView];
    [self.backView addSubview:self.eraserRectView];
    self.oneRectView = [self setupRectView];
    self.oneRectView.hidden = NO;
    [self.backView addSubview:self.oneRectView];
    self.twoRectView = [self setupRectView];
    [self.backView addSubview:self.twoRectView];
    self.threeRectView = [self setupRectView];
    [self.backView addSubview:self.threeRectView];
    self.fourRectView = [self setupRectView];
    [self.backView addSubview:self.fourRectView];
}


- (void)clickAlertSaveButton{
    
    NSArray * arr = DEF_GET_OBJECT(@"image");
    NSMutableArray * imageArr = [NSMutableArray arrayWithArray:arr];
    NSData *imageData = UIImageJPEGRepresentation(self.image,1.0f);
    [imageArr addObject:imageData];
    DEF_SET_OBJECT(imageArr, @"image");
    
    [self clickCloseButton];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)clickAlertShareButton{
    
    NSString *testToShare = @"My Paintings";
    UIImage *imageToShare = self.image;
    NSArray *activityItems = @[testToShare,imageToShare];
     UIActivityViewController *activityVc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityVc animated:YES completion:nil];
}

//点击按钮方法,这里容易犯错
-(void)colorBtnClick:(UIButton *)sender{
    
    for (UIButton * btn in self.colorView.subviews) {
        
        if (btn == sender) {
            
            if (!btn.selected) {
                
                btn.selected = YES;
            }
        }else{
            
            btn.selected = NO;
        }
    }
    [self.drawView setStrokeColor:[UIColor colorWithHexString:self.colorArray[sender.tag]]];
}


- (void)clickSaveButton{

    //1.开启一个位图上下文
    UIGraphicsBeginImageContextWithOptions(self.drawView.bounds.size, NO, 1);
    //2.把画板上的内容渲染到上下文当中
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    [self.drawView.layer renderInContext:ctx];
    //3.从上下文当中取出一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //4.关闭上下文
    UIGraphicsEndImageContext();

    self.image = newImage;
    
    [self setupToolBarView];
}

-(void)clickToolButton:(UIButton *)button{
    
    if (button == self.eraserButton) {
        
        self.eraserRectView.hidden = NO;
        
        self.oneRectView.hidden = YES;
        self.twoRectView.hidden = YES;
        self.threeRectView.hidden = YES;
        self.fourRectView.hidden = YES;
        [self.drawView eraseSreen];
    }else if (button == self.penOneButton){
        
        self.eraserRectView.hidden = YES;
        
        self.oneRectView.hidden = NO;
        self.twoRectView.hidden = YES;
        self.threeRectView.hidden = YES;
        self.fourRectView.hidden = YES;
        [self.drawView setStrokeWidth:2];
    }else if (button == self.penTwoButton){
        
        self.eraserRectView.hidden = YES;
        
        self.oneRectView.hidden = YES;
        self.twoRectView.hidden = NO;
        self.threeRectView.hidden = YES;
        self.fourRectView.hidden = YES;
        [self.drawView setStrokeWidth:5];
    }else if (button == self.penThreeButton){
        
        self.eraserRectView.hidden = YES;
        
        self.oneRectView.hidden = YES;
        self.twoRectView.hidden = YES;
        self.threeRectView.hidden = NO;
        self.fourRectView.hidden = YES;
        [self.drawView setStrokeWidth:7];
    }else{
        
        self.eraserRectView.hidden = YES;
        
        self.oneRectView.hidden = YES;
        self.twoRectView.hidden = YES;
        self.threeRectView.hidden = YES;
        self.fourRectView.hidden = NO;
        [self.drawView setStrokeWidth:10];
    }
}


- (void)clickBackButton{
    
    if (self.drawView.stroks.count == 0) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                                  message:@"Do you want to save the picture？"
                                                                           preferredStyle:UIAlertControllerStyleAlert];
                   
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault
        handler:^(UIAlertAction * action) {
                                                                             
           //1.开启一个位图上下文
           UIGraphicsBeginImageContextWithOptions(self.drawView.bounds.size, NO, 1);
           //2.把画板上的内容渲染到上下文当中
           CGContextRef ctx =  UIGraphicsGetCurrentContext();
           [self.drawView.layer renderInContext:ctx];
           //3.从上下文当中取出一张图片
           UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
           //4.关闭上下文
           UIGraphicsEndImageContext();

           self.image = newImage;
           
           [self setupToolBarView];
        }];
        
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
        handler:^(UIAlertAction * action) {
            
             [self.navigationController popViewControllerAnimated:YES];
        }];
                   
        [alert addAction:defaultAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
   
}

- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backButton);
        make.trailing.mas_equalTo(self.view).offset(-90);
        make.width.mas_equalTo(68);
        make.height.mas_equalTo(36);
    }];
    
    [self.colorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(45);
        make.top.mas_equalTo(self.backButton.mas_bottom).offset(10);
        make.bottom.mas_equalTo(self.view).offset(-20);
        make.width.mas_equalTo(55);
    }];
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.colorView.mas_trailing).offset(15);
        make.centerY.height.mas_equalTo(self.colorView);
        make.trailing.mas_equalTo(self.view).offset(-55);
    }];
    
    [self.drawView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.top.bottom.mas_equalTo(self.backView);
        make.leading.mas_equalTo(self.backView).offset(80);
    }];
    
    [self.eraserButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(15);
        make.bottom.mas_equalTo(self.backView).offset(-25);
        make.width.mas_equalTo(32);
        make.height.mas_equalTo(41);
    }];
    
    [self.penOneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.eraserButton);
        make.bottom.mas_equalTo(self.eraserButton.mas_top);
        make.width.mas_equalTo(41);
        make.height.mas_equalTo(32);
    }];
    
    [self.penTwoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.width.mas_equalTo(self.penOneButton);
        make.bottom.mas_equalTo(self.penOneButton.mas_top);
    }];
    
    [self.penThreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.width.mas_equalTo(self.penTwoButton);
        make.bottom.mas_equalTo(self.penTwoButton.mas_top);
    }];
    
    [self.penFourButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.width.mas_equalTo(self.penThreeButton);
        make.bottom.mas_equalTo(self.penThreeButton.mas_top);
    }];
    
    [self.eraserRectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10);
        make.centerY.mas_equalTo(self.eraserButton);
        make.height.width.mas_equalTo(4);
    }];
    
    [self.oneRectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.width.mas_equalTo(self.eraserRectView);
        make.centerY.mas_equalTo(self.penOneButton);
    }];
    
    [self.twoRectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.width.mas_equalTo(self.oneRectView);
        make.centerY.mas_equalTo(self.penTwoButton);
    }];
    
    [self.threeRectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.width.mas_equalTo(self.twoRectView);
        make.centerY.mas_equalTo(self.penThreeButton);
    }];
    
    [self.fourRectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.width.mas_equalTo(self.threeRectView);
        make.centerY.mas_equalTo(self.penFourButton);
    }];
}


-(void)setupColorButton
{
    float viewHeight = kScreenH - 86;
    float Start_X = 19.0f;   // 第一个按钮的X坐标
    float Start_Y = 10;    // 第一个按钮的Y坐标
    float Height_Space = (viewHeight - 224) / 11;     // 竖间距
    float Button_Height = 17.0f;    // 高
    for (int i = 0 ; i < 12; i++) {
        // 圆角按钮
        UIButton *mapBtn = [UIButton new];
        mapBtn.tag = i;//这句话不写等于废了
        mapBtn.frame = CGRectMake(Start_X, i  * (Button_Height + Height_Space)+Start_Y, Button_Height, Button_Height);
        mapBtn.layer.cornerRadius = 17/2;
        mapBtn.layer.masksToBounds = YES;
        mapBtn.backgroundColor = [UIColor colorWithHexString:self.colorArray[i]];
        [mapBtn setImage:[UIImage imageNamed:@"gou"] forState:UIControlStateSelected];
        if (i == 0) {
            mapBtn.selected = YES;
        }
        [self.colorView addSubview:mapBtn];
      //按钮点击方法
        [mapBtn addTarget:self action:@selector(colorBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}


- (void)setupToolBarView{

    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //样式
    toolbar.barStyle = UIBarStyleBlack;//半透明
    //透明度
    toolbar.backgroundColor = ZRBlackColor;
    toolbar.alpha = 0.6f;

    self.toolbar = toolbar;
    [self.navigationController.view addSubview:self.toolbar];
    
    UIView * whiteView = [[UIView alloc] init];
    
    whiteView.backgroundColor = ZRWhiteColor;
    whiteView.userInteractionEnabled = YES;
    whiteView.layer.cornerRadius = 20;
    whiteView.layer.borderWidth = 2;
    whiteView.layer.borderColor = [ZRBlackColor CGColor];
    whiteView.layer.masksToBounds = YES;
    
    self.whiteView = whiteView;
    
    [self.navigationController.view addSubview:self.whiteView];

    
    UIButton * closeButton = [UIButton new];
    
    [closeButton setImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(clickCloseButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.whiteView addSubview:closeButton];
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.backgroundColor = ZRWhiteColor;
    imageView.image = self.image;
    
    [self.whiteView addSubview:imageView];
    
    
    UIButton * saveButton = [UIButton new];
    
    [saveButton setTitle:@"save" forState:UIControlStateNormal];
    [saveButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(clickAlertSaveButton) forControlEvents:UIControlEventTouchUpInside];
    [saveButton setBackgroundColor:[UIColor colorWithHexString:@"EDFF1C"]];
    saveButton.layer.cornerRadius = 15;
    saveButton.layer.borderWidth = 2;
    saveButton.layer.borderColor = [ZRBlackColor CGColor];
    
    [self.whiteView addSubview:saveButton];
    
    
    UIButton * shareButton = [UIButton new];
    
    [shareButton setTitle:@"share" forState:UIControlStateNormal];
    [shareButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [shareButton addTarget:self action:@selector(clickAlertShareButton) forControlEvents:UIControlEventTouchUpInside];
    [shareButton setBackgroundColor:[UIColor colorWithHexString:@"EDFF1C"]];
    shareButton.layer.cornerRadius = 15;
    shareButton.layer.borderWidth = 2;
    shareButton.layer.borderColor = [ZRBlackColor CGColor];
    
    [self.whiteView addSubview:shareButton];
    
    
    [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view);
        make.width.mas_equalTo(510);
        make.height.mas_equalTo(290);
    }];
    
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.whiteView).offset(-15);
        make.top.mas_equalTo(self.whiteView).offset(15);
        make.width.height.mas_equalTo(44);
    }];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.whiteView);
        make.top.mas_equalTo(self.whiteView).offset(45);
        make.height.mas_equalTo(150);
        make.width.mas_equalTo(250);
    }];
    
    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.whiteView).offset(120);
        make.bottom.mas_equalTo(self.whiteView).offset(-20);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(45);
    }];

    [shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.whiteView).offset(-120);
        make.centerY.width.height.mas_equalTo(saveButton);
    }];
}


- (void)clickCloseButton{
    
    [self.toolbar removeFromSuperview];
    [self.whiteView removeFromSuperview];
}


- (UIButton *)setupButtonWithImg:(NSString *)img{
    
    UIButton * button = [UIButton new];
    [button setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
    button.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    [button addTarget:self action:@selector(clickToolButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


-(UIView *)setupRectView{
    
    UIView * rectView = [UIView new];
    
    rectView.backgroundColor = [UIColor redColor];
    rectView.layer.cornerRadius = 2;
    rectView.layer.masksToBounds = YES;
    rectView.hidden = YES;
    
    return rectView;
}


- (NSArray *)colorArray{
    
    if (!_colorArray) {
        
        _colorArray = @[@"E60012",@"E65601",@"FFF100",@"65EB5C",@"0AA000",@"ED73CB",@"A50FB0",@"1D22BA",@"39A0FC",@"9D7264",@"58393A",@"000000"];
    }
    
    return _colorArray;
}

@end
